package com.ruoyi.kenuo.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.kenuo.service.IEquService;
import com.ruoyi.kenuo.service.IKernelVerisonService;
import com.ruoyi.kenuo.service.IKernelVersionValueService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


@RequestMapping("/kenuo/open")
@RestController
public class OpenController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private IEquService equService;

    @Autowired
    private IKernelVersionValueService kernelVersionValueService;
    @Autowired
    private IKernelVerisonService kernelVerisonService;


    @Value(value = "${ruoyi.dips}")
    private String dips;

    @Value("${ruoyi.httpLu}")
    private String httpLu;

    /**
     * 根据username查询当前用户名
     */
    @ApiOperation("根据username查询当前用户")
    @ApiImplicitParam(name = "userName", value = "用户名", required = true, dataType = "string", dataTypeClass = String.class)
    @GetMapping("/getUserByName")
    public AjaxResult getUserByUserName(String userName) {
        if (StrUtil.isEmpty(userName)) {
            return AjaxResult.error("未获取到参数userName");
        }
        AjaxResult ar = new AjaxResult();
        SysUser user = userService.getUserByUserName(userName);
        if (ObjectUtil.isNotNull(user)) {
            ar.put("flagType", true);
            ar.put("code", 200);
            return ar;
        } else {
            ar.put("flagType", false);
            ar.put("code", 200);
            return ar;
        }

    }

    /**
     * 前端上传文件
     */
    @PostMapping("/upWenjian")
    public AjaxResult upWenjian(@RequestParam("file") MultipartFile file) throws Exception
    {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        long size = file.getSize();
        Map<String,Object> mp = new LinkedHashMap<>();
        mp.put("size",size);
        mp.put("fileName",httpLu+fileName);
        return AjaxResult.success(mp);
    }
    /**
     * 通用删除文件的方法,需要携带约定密码验证
     */
    @PostMapping("/deleteFile")
    public AjaxResult  deleteFile(@RequestBody Map map){
        String curDips = (String) map.get("dips");
        String filePath = (String) map.get("filePath");
        String userId = "";
        if(map.containsKey("userId")){
            userId =  map.get("userId").toString();
        }
        Boolean bl = this.disMatch(curDips);
        if (bl) {
            return AjaxResult.error("验签失败！");
        }
        if(filePath.contains("profile")){
            int t = filePath.indexOf("profile");
            String fp = filePath.substring(t+14);
            filePath = RuoYiConfig.getUploadPath() + fp;
        }
        //删除用户的url数据
        if(StringUtils.isNotEmpty(userId)){
            SysUser user = new SysUser();
            user.setUserId(Long.valueOf(userId));
            user.setUrl("");
            userService.updateUrlUser(user);
        }
        boolean b = FileUtils.deleteFile(filePath);
        if(b){
            return AjaxResult.success("操作成功！");
        }else {
            return AjaxResult.error("操作失败！");
        }
    }
    public Boolean disMatch(String curDips) {
        AjaxResult aj = new AjaxResult();
        if (StringUtils.isEmpty(curDips) || !curDips.equals(dips)) {
            return true;
        } else {
            return false;
        }
    }

}
